package com.bdqn.qnnews.search.service.impl;

import com.bdqn.qnnews.common.constants.MQConstants;
import com.bdqn.qnnews.search.entity.AppInfoArticle;
import com.bdqn.qnnews.search.service.ApArticleSearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @author xlzhang
 * @date 2024/1/10 11:16
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class ArticleAddOrRemoveListener{

    private final ApArticleSearchService searchService;

    // 让容器自动管理和创建内部类对象，并注入容器
    @Component
    // mq监听注解
    @RocketMQMessageListener(
            consumerGroup = "search_group",
            topic = MQConstants.QN_NEWS_UP_ES_TOPIC
    )
    public class ArticleAddListener implements RocketMQListener<AppInfoArticle> {
        @Override
        public void onMessage(AppInfoArticle article) {
            searchService.saveAndUpdateArticle(article);
            log.debug("同步文章数据到ES成功！文章id={}",article.getId());
        }
    }
}
